package com.zoke.password.ui;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zoke.password.R;
import com.zoke.password.base.BaseActivity;
import com.zoke.password.model.Password;
import com.zoke.password.tools.PwdCheckUtil;
import com.zoke.password.tools.UToast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_password_detail)
public class PasswordDetailActivity extends BaseActivity {

    @ViewInject(R.id.tv_title)
    TextView tv_title;
    @ViewInject(R.id.iv_back)
    ImageView iv_back;

    private Password password;
    @ViewInject(R.id.et_account)
    TextView et_account;
    @ViewInject(R.id.et_password)
    TextView et_password;
    @ViewInject(R.id.et_tip)
    TextView et_tip;
    @ViewInject(R.id.tv_strong)
    TextView tv_strong;

    public static void open(Activity activity, Password password) {
        if (password == null) return;
        Intent intent = new Intent(activity, PasswordDetailActivity.class);
        intent.putExtra("json", new Gson().toJson(password));
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iv_back.setVisibility(View.VISIBLE);
        String json = getIntent().getStringExtra("json");
        if (TextUtils.isEmpty(json))
            return;
        password = new Gson().fromJson(json, Password.class);
        tv_title.setText(password.title);
        et_account.setText(password.account);
        et_password.setText(password.password);
        et_tip.setText(password.tips);
        PwdCheckUtil.LEVEL sng = PwdCheckUtil.checkPassword(password.password);
        String tips = getString(R.string.password_strong);
        int levelColor;
        switch (sng) {
            case EASY:
                tips = String.format(tips, getString(R.string.password_easy)) + "，建议更换";
                levelColor = R.color.level_1;
                break;
            case MIDIUM:
                tips = String.format(tips, getString(R.string.password_mid));
                levelColor = R.color.level_2;
                break;
            case STRONG:
                tips = String.format(tips, getString(R.string.password_str));
                levelColor = R.color.level_3;
                break;
            case EXTREMELY_STRONG:
            case VERY_STRONG:
            default:
                tips = String.format(tips, getString(R.string.password_very));
                levelColor = R.color.level_4;
                break;
        }
        tv_strong.setText(tips);
        tv_strong.setBackgroundColor(getResources().getColor(levelColor));
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        if (mImmersionBar != null)
            mImmersionBar.statusBarDarkFont(true)
                    .statusBarView(R.id.status)
                    .init();
    }

    @Event({R.id.iv_back, R.id.btn_create, R.id.btn_save})
    private void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.btn_create:
                copy(password.account);
                break;
            case R.id.btn_save:
                copy(password.password);
                break;
        }
    }

    public void copy(String tip) {
        ClipboardManager mClipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData mClipData = ClipData.newPlainText("Label", tip);         //‘Label’这是任意文字标签
        mClipboardManager.setPrimaryClip(mClipData);
        UToast.show(this, "成功复制到剪贴板");
    }

}
